﻿using System;
using System.Collections.Generic;

namespace SplendidCRM.Ext
{
    public static class Db
    {
        public static Int32 Execute(String sql, params Object[] parameters)
        {
            return new Query().Execute(sql, parameters);
        }

        public static T QueryValue<T>(String sql, Func<Object, T> converter, params Object[] parameters)
        {
            return new Query().Value(sql, converter, parameters);
        }

        public static T QueryValue<T>(String sql, params Object[] parameters)
        {
            return new Query().Value<T>(sql, parameters);
        }

        public static Object QueryValue(String sql, params Object[] parameters)
        {
            return new Query().Value(sql, parameters);
        }

        public static dynamic QuerySingle(String sql, params Object[] parameters)
        {
            return new Query().Single(sql, parameters);
        }

        public static Int64 Insert(Object obj, String table)
        {
            return new Query().Insert(obj, table);
        }

        public static Int32 Insert(String table, params Object[] objs)
        {
            return new Query().Insert(table, objs);
        }


        public static Int32 Update<TObject>(TObject values, String table, params Func<TObject, Object>[] conditions)
        {
            return new Query().Update(values, table, conditions);
        }

        public static Int32 Update(String table, Object values, String condition, params Object[] parameters)
        {
            return new Query().Update(table, values, condition, parameters);
        }

        public static Int32 Delete(String table, String condition, params Object[] parameters)
        {
            return new Query().Delete(table, condition, parameters);
        }

        public static Int32 Delete(String table)
        {
            return new Query().Delete(table);
        }

        public static IEnumerable<dynamic> QueryLimit(int start, int count, out int total, string sql, params object[] parameters)
        {
            return new Query().Limit(start, count, out total, sql, parameters);
        }

        public static IEnumerable<dynamic> QueryLimit(int count, string sql, params object[] parameters)
        {
            return new Query().Limit(count, sql, parameters);
        }

        public static IEnumerable<dynamic> QueryLimit(int start, int count, string sql, params object[] parameters)
        {
            return new Query().Limit(start, count, sql, parameters);
        }

        public static IEnumerable<dynamic> Query(String sql, params Object[] parameters)
        {
            return new Query().Multiple(sql, parameters);
        }

        public static Boolean Transaction(Action<Query> callback)
        {
            return Transaction(q =>
                                   {
                                       callback(q);
                                       return true;
                                   });
        }

        public static TResult Transaction<TResult>(Func<Query, TResult> callback)
        {
            if (callback == null) throw new ArgumentNullException("callback");
            using(var query = new Query())
            {
                query.Begin();
                var result = callback(query);
                query.Commit();
                return result;
            }
        }
    }
}